﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using WPFExcelReport.Controls;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ChartEx.xaml
    /// </summary>
    public partial class TMENU
    {
        #region ==IsMenuTree===========
        public static readonly DependencyProperty IsMenuTreeProperty = DependencyProperty.Register("IsMenuTree",
            typeof(bool), typeof(TMENU),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsMenuTreeChanged));
        public bool IsMenuTree
        {
            get
            {
                return (bool)GetValue(IsMenuTreeProperty);
            }
            set
            {
                SetValue(IsMenuTreeProperty, value);
            }
        }
        private static void OnIsMenuTreeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TMENU;
            if (src != null)
            {
                src.IsMenuTree = (bool)e.NewValue;
            }
        }
        #endregion
        //获取该用户菜单并显示
        public TMENU()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_MENUDEF();
            this.DataContext = MV;
            InitializeComponent();
            EventDispatcher.Instance.OnMessage += wf_OnMessage;

        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        void wf_OnMessage(object sender, MessageEventArgs e)
        {
            if (e.Info.Where != "WORKFLOW")
                return; 
            PM["DOCTYPE"] = e.Info.What;
            PM["DEFAULTUI"] = e.Info.P1;
            PM["UI"] = e.Info.Details;
            PM["EVENTS"] = e ;
            NormalTask = Task.Factory.StartNew(() => StartDocument(PM), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

            //获取界面，获取工作流事件参数，创建界面，传递这个工作流ID
            //如果界面已经存在，则仅仅赋值
        }
        public void StartDocument(Dictionary<string,object>pm)
        {
            try
            {
                DocumentBase doc = ActivateReportByDocType(pm); 
                doc.OnWorkflowIDChange((MessageEventArgs)pm["EVENTS"]);

            }
            catch(Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }
        
        public override void LOADBYAUF()
        {
            try
            {
                TBMain.Clear();
                if (ORDERNUM.Length < 2)
                    return;
                ResetMenu();
                SetUpUserMenu(true);
                SResult = TMV.PROG.LOADALL();
                TBPRG = SResult.Tables[0].Copy();
                PM["USERID"] = app.User;
                PM["COMPANY"] = app.company;
                SResult = TMV.UR.LOADBYUSER(PM);
                TBUR = SResult.Tables[0].Copy();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("请检查服务器是否联通");
            }
        }
        private void ResetMenu()
        {
            WrapPanel cmom = (WrapPanel)this.FindName("gpArticle");
            if (cmom != null) { cmom.Children.Clear(); IsgpArticle=false; }
            cmom = (WrapPanel)this.FindName("gpPlan");
            if (cmom != null) { cmom.Children.Clear(); IsgpPlan=false; }
            cmom = (WrapPanel)this.FindName("gpProd");
            if (cmom != null) { cmom.Children.Clear(); IsgpPlan=false; }
            cmom = (WrapPanel)this.FindName("gpSales");
            if (cmom != null) { cmom.Children.Clear(); IsgpSales = false; }
            cmom = (WrapPanel)this.FindName("gpFinish");
            if (cmom != null) { cmom.Children.Clear(); IsgpFinish = false; }
            cmom = (WrapPanel)this.FindName("gpFinishing");
            if (cmom != null) { cmom.Children.Clear(); IsgpFinishing = false; }
            cmom = (WrapPanel)this.FindName("gpDye");
            if (cmom != null) { cmom.Children.Clear(); IsgpDye = false; }
            cmom = (WrapPanel)this.FindName("gpSizing");
            if (cmom != null) { cmom.Children.Clear(); IsgpSizing = false; }
            cmom = (WrapPanel)this.FindName("gpWeaving");
            if (cmom != null) { cmom.Children.Clear(); IsgpWeaving = false; }
            cmom = (WrapPanel)this.FindName("gpSample");
            if (cmom != null) { cmom.Children.Clear(); IsgpSample = false; }
            cmom = (WrapPanel)this.FindName("gpEK");
            if (cmom != null) { cmom.Children.Clear(); IsgpEK = false; }
            //cmom = (WrapPanel)this.FindName("gpMaterial");
            // cmom.Children.Clear();
            cmom = (WrapPanel)this.FindName("gpWarehouse");
            if (cmom != null) { cmom.Children.Clear(); IsgpWarehouse = false; }
            cmom = (WrapPanel)this.FindName("gpWS03");
            if (cmom != null) { cmom.Children.Clear(); IsgpWS03 = false; }
            cmom = (WrapPanel)this.FindName("gPONe");
            if (cmom != null) { cmom.Children.Clear(); IsgPONe = false; }
            cmom = (WrapPanel)this.FindName("gpINOUTINV");
            if (cmom != null) { cmom.Children.Clear(); IsgpINOUTINV = false; }
            cmom = (WrapPanel)this.FindName("gpSystem");
            if (cmom != null) { cmom.Children.Clear(); IsgpSystem = false; }
        }
        public void SetUpUserMenu(bool IsTranslate)
        {
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            dwData["COMPANY"] = app.company;
            dwData["USERID"] = app.User; 
            SResult = TMV.QueryICC_MENUDEF(dwData, 2);
            MergeData(SResult, false, true);
       
            Control ct = null; 
            TreeViewItem item = new TreeViewItem();
            foreach (DataRow dwUMenu in TBMain.Rows)
            {
                string com = dwUMenu["COMPANY"].ToString().Trim();
                string proc = dwUMenu["PROCESS"].ToString().Trim();
                string menuitem = dwUMenu["MENUITEM"].ToString().Trim();
                string father = dwUMenu["FATHER"].ToString().Trim();
                if (proc == "VISIBLE")
                {
                    ct = (Control)this.FindName(menuitem);
                    if (ct != null)
                    {
                        ct.IsEnabled = true;
                        ct.Visibility = Visibility.Visible;
                    }
                    ct = (Control)this.FindName("t" + menuitem);
                    if (ct != null)
                    {
                        ct.IsEnabled = true;
                        ct.Visibility = Visibility.Visible;
                    }
                }
                if (proc != "MENU")
                    continue;
                object control = this.FindName(father);
                if (control == null)
                    continue;
                if (!control.GetType().ToString().Contains("Wrap"))
                {
                    continue;
                }
                #region==Father========
                WrapPanel cf = (WrapPanel)control;
                if (cf == null)
                    continue;
                switch (father)
                {
                    case "gpArticle":
                        IsgpArticle = true;  
                        break;
                    case "gpPlan":
                        IsgpPlan = true; 
                        break;
                    case "gpProd":
                        IsgpProd = true; 
                        break;
                    case "gpSales": 
                        IsgpSales = true;
                        break;
                    case "gpFinish": 
                        IsgpFinish = true;
                        break;
                    case "gpFinishing": 
                        IsgpFinishing = true;
                        break;
                    case "gpDye": 
                        IsgpDye = true;
                        break;
                    case "gpSizing": 
                        IsgpSizing = true;
                        break;
                    case "gpWeaving": 
                        IsgpWeaving = true;
                        break;
                    case "gpSample": ;
                        IsgpSample = true;
                        break;
                    case "gpEK": 
                        IsgpEK = true;
                        break;
                    case "gpMaterial": 
                        IsgpMaterial = true;
                        break;
                    case "gpWarehouse": 
                        IsgpWarehouse = true;
                        break;
                    case "gpWS03": 
                        IsgpWS03 = true;
                        break;
                    case "gPONe": 
                        IsgPONe = true;
                        break;
                    case "gpINOUTINV": 
                        IsgpINOUTINV = true;
                        break;
                    case "gpSystem": 
                        IsgpSystem = true;
                        break;

                }
                bool isFound = false;
                var qitem=from TreeViewItem tc in menuTree.Items
                          where tc.Tag.ToString()==father
                          select tc;
                foreach(TreeViewItem tc in qitem)
                {
                    isFound = true;
                    item = tc;
                }
                if(isFound)
                {
                    TBTMENU cb1 = new TBTMENU();
                    cb1.Header = Helper.GetGB2312Value(dwUMenu["HEADER"].ToString().Trim());
                    if (IsTranslate)
                    {
                        cb1.Header = app.StartTranslate(cb1.Header);
                    }
                    cb1.KEY = dwUMenu["TAG"].ToString().Trim(); 
                    item.Items.Add(cb1);
                }
                          #endregion==========
                TBTN cb = new TBTN();
                string header = Helper.GetGB2312Value(dwUMenu["HEADER"].ToString().Trim());
                cb.Header = header;
                if (IsTranslate)
                {
                    cb.Header = app.StartTranslate(header);
                }
                cb.KEY = dwUMenu["TAG"].ToString().Trim(); 
                cf.Children.Add(cb);
            }
        }
    }
}
